<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-09-11
 * Time: 14:36
 */

namespace app\admin\controller;


use app\constant\StoresConst;
use app\model\AdminStores;
use app\util\ReturnCode;
use think\exception\DbException;

class Stores extends Base
{
    /**
     * 根据id获取店铺数据
     * @return array
     */
    public function get()
    {
        $id = request()->param('id');
        try {
            $data = (new AdminStores())->where(['id' => $id])->find();
            return $this->buildSuccess($data);
        } catch (DbException $e) {
        }
        return $this->buildFailed(ReturnCode::INVALID, '获取数据失败');
    }

    /**
     * 店铺添加
     */
    public function save()
    {
        $post = $this->request->post();
        $post= $this->addTid($post);
        //普通状态
        $post['status'] = StoresConst::STATUS_NORMAL;
        $post['level'] = StoresConst::LEVEL_NORMAL;
        $res = AdminStores::create($post);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }
    /**
     * 店铺编辑
     */
    public function edit()
    {
        $postData = $this->request->except('status,create_time,level');
        $res = AdminStores::update($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }
    /**
     * 删除店铺
     * @return array
     */
    public function del()
    {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        $model = AdminStores::get($id);
        if(!$model){
            return $this->buildFailed(ReturnCode::FAILED, '不存在的店铺');
        }
        if($model->level===StoresConst::LEVEL_SELF){
            return $this->buildFailed(ReturnCode::FAILED, '不可删除自营店铺');
        }
        AdminStores::update(['id'=>$id,'del'=>1]);
        return $this->buildSuccess([]);
    }

    /**
     * 获取列表
     * @return array
     * @throws \think\exception\DbException
     * @author chniccs
     */
    public function getList()
    {

        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $type = $this->request->get('type', '', 'intval');
        $keywords = $this->request->get('keywords', '');
        $obj = new AdminStores();
        if ($type) {
            switch ($type) {
                case 1:
                    $obj = $obj->whereLike('name', "%{$keywords}%");
                    break;
                case 2:
                    $obj = $obj->whereLike('phone', "%{$keywords}%");
                    break;
            }
        }
        $where = [];
        $where = $this->addTid($where);
        $listObj = $obj->where($where)->order('create_time DESC')->field('id,phone,address,name,create_time,expiration_time,level,status')->paginate($limit, false, ['page' => $start])->toArray();

        return $this->buildSuccess([
            'list' => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }
}